package com.bsh.controller.user;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.dto.UserListQryDto;
import com.bsh.service.UserVerifyService;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

@Controller
public class UserValidController {
	private static Logger log = LoggerFactory.getLogger(UserValidController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	private UserVerifyService veriService;

	/**
	 * 身份验证列表
	 */
	@RequestMapping(value = "/user/valid/list")
	public ModelAndView validList(@ModelAttribute UserListQryDto dto) {
		//String userName = request.getParameter("userName") == null ? null : request.getParameter("userName").toString();
		//String mobile = request.getParameter("mobile") == null ? null : request.getParameter("mobile").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/user/validList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = veriService.getPage(pager, dto);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		//mv.addObject("userName", userName);
		//mv.addObject("mobile", mobile);
		mv.addObject("dto", dto);
		return mv;
	}
	

	/**
	 * 审核通过
	 */
	@ResponseBody
	@RequestMapping(value = "/user/audit/valid/pass/{id}")
	public PageJosnMessage pass(@PathVariable(value = "id") Integer id) {
		Integer res = veriService.audit(id, true, null);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}

	/**
	 * 添加审核消息
	 */
	@ResponseBody
	@RequestMapping(value = "/user/audit/valid/refuse/{id}")
	public ModelAndView refuseMsg(@PathVariable(value = "id") Integer id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/user/auditMsg");
		mv.addObject("id", id);
		return mv;
	}
	
	/**
	 * 审核未通过
	 */
	@ResponseBody
	@RequestMapping(value = "/user/audit/valid/refuse")
	public ModelAndView refuse(@RequestParam(value = "id") Integer id, @RequestParam(value = "msg") String auditMsg) {
		Integer res = veriService.audit(id, false, auditMsg);
		ModelAndView mv = refuseMsg(id);
		mv.addObject("id", id);
		mv.addObject("msg", auditMsg);

		String msg = "";
		if (res == 1)
			msg = PageTools.alertCloseFraRefresh("手持证件认证审核", "审核成功！", 1000);
		else
			msg = PageTools.alert("手持证件认证审核", "审核失败，请稍后重试！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}
	
}
